package com.sgb.kjwl.viewmodel.enterprise;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ContactsEntitiy;
import com.swgk.core.mmkvutil.CacheMarker;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.mmkvutil.MkvUtils;
import com.swgk.core.mmkvutil.DatalUtils;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddContanctsViewModel extends AndroidViewModel {
    public MutableLiveData<String> path = new MutableLiveData<>();
    public MutableLiveData<Boolean> status = new MutableLiveData<>();
    public MutableLiveData<ContactsEntitiy> data = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> areaData = new MutableLiveData<>();
    public MutableLiveData<String> toastShow = new MutableLiveData<>();
    //1-show,2-dismiss
    public MutableLiveData<Integer> showLoading = new MutableLiveData<>();

    public MutableLiveData<String> commitStatus = new MutableLiveData<>();

    public AddContanctsViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void submitData( int type) {
        showLoading.postValue(1);
        NewAPIManager.getInstance().getEnterPrisenewAPI().saveOrUpdate(data.getValue()).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                showLoading.postValue(2);
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {

                    if(type>0){
                        commitStatus.postValue("操作成功");
                    }else{
                        commitStatus.postValue(response.body().getMsg());
                    }
                } else {
                    toastShow.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                showLoading.postValue(2);
                toastShow.postValue(t.getMessage());
            }
        });

    }

    public void getAreaData() {
        showLoading.postValue(1);
        Type type = new TypeToken<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>() {
        }.getType();
        Object obj = DatalUtils.getInstance().getData(MkvUtils.CacheModular, CacheMarker.AREA_JSON, type);
        if (obj != null) {
            showLoading.postValue(2);
            List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> dataList = (List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>) obj;
            areaData.postValue(dataList);
        } else {
            APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                    showLoading.postValue(2);
                    if (response.body() == null) {
                        return;
                    }
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        areaData.postValue(response.body().getData());
                    } else {
                        toastShow.postValue(response.body().getMsg());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                    showLoading.postValue(2);
                    toastShow.postValue(t.getMessage());
                }
            });
        }
    }
}
